<?php

namespace Home\Controller;

use Think\Controller;

class LoginController extends Controller
{
    protected $article = null;

    public function _initialize()
    {
        $this->user= D('User');
    }

    public function index(){
        $this->login();
    }
    public function login(){
        $this->display('login');
    }

    public function checkLogin(){
        $account = I('post.account');
        $password = I('post.password');
        if(empty($account) || empty($password)){
            $this->ajaxCallMsg(1,'账号和密码不为空！');

        }
        $row = $this->user->check_login($account,$password);
        if(!$row){
            $this->ajaxCallMsg(2,'账号或密码错误！');
        }

        $userInfo = json_encode($row);
        $userInfo = tbs_new_encode($userInfo);  //加密
        //失效时间1 小时
        $option = array('session_name '=>'userInfo','path'=>'/','expire'=>3600);
        session($option);
        session('userInfo',$userInfo);
        $this->user->updateTime($row['id']);
        return $this->ajaxCallMsg(0,'登录成功');
    }

    public function ajaxCallMsg($error_code = 0, $msg = '', $data = array())
    {
        $returnData = array(
            'error_code' => $error_code,
            'msg' => $msg
        );

        if ($data) {
            $returnData['data'] = $data;
        }

        $this->ajaxReturn($returnData);
    }




}